package cn.com.kh.batch;

import cn.com.kh.KHUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Title: cn.com.dev-DevTao
 * @Description:ngsapitask-ph 系统API接口开发Demo，重点关注业务逻辑部分
 * @Author: lijun
 * @Date 2021-12-03 10:23
 */
public class BatchData_Utils {
    private static final Log log = LogFactory.getLog(BatchData_Utils.class);

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     * 更新状态-1
     */
    public static void updateStep(JdbcTemplate jt, String step, String recGuid) {
        String updSql = "Update Ph_Contract_Sign_List Set Step = ? Where Rec_Guid = ?";
        int i_Num = jt.update(updSql, step, recGuid);
        log.debug("❕Ph_Contract_Sign_List更新Step状态 recGuid:" + recGuid + " step:" + step + " i_Num:" + i_Num);
    }

    /**
     * 更新状态-1
     */
    public static void updateStep_State(JdbcTemplate jt, String step, String step_state, String recGuid) {
        String updSql = "Update Ph_Contract_Sign_List Set Step = ?, Step_State = ? Where Rec_Guid = ?";
        int i_Num = jt.update(updSql, step, step_state, recGuid);
        log.debug("❕Ph_Contract_Sign_List更新Step_State状态 recGuid:" + recGuid + " step:" + step + " step_state:" + step_state + " i_Num:" + i_Num);
    }

    /**
     * 更新状态-1
     */
    public static void updateStep_APICode(JdbcTemplate jt, String step_state, String api_code, String api_msg, String recGuid) {
        String updSql = "Update Ph_Contract_Sign_List Set Step_State = ?, Api_Code = ?, Api_Msg = ? Where Rec_Guid = ?";
        int i_Num = jt.update(updSql, step_state, api_code, api_msg + " Time:" + sdf.format(new Date()), recGuid);
        log.debug("❕Ph_Contract_Sign_List更新状态 recGuid:" + recGuid + " step_state:" + step_state + " api_code:" + api_code + " api_msg:" + api_msg + " i_Num:" + i_Num);
    }

    /**
     * 更新状态-2
     */
    public static void updateStep_All(JdbcTemplate jt, String step, String step_state, String api_code, String api_msg, String recGuid) {
        String updSql = "Update Ph_Contract_Sign_List Set Step = ?, Step_State = ?, Api_Code = ?, Api_Msg = ? Where Rec_Guid = ?";
        int i_Num = jt.update(updSql, step, step_state, api_code, api_msg + " Time:" + sdf.format(new Date()), recGuid);
        log.debug("‼Ph_Contract_Sign_List更新状态(处理阶段) recGuid:" + recGuid + " step:" + step + " step_state:" + step_state + " api_code:" + api_code + " api_msg:" + api_msg + " i_Num:" + i_Num);
    }

    // 记录系统-接口操作交互日志信息Log
    public static void toSys_Oper_Log(JdbcTemplate jt, String Rec_Guid, String Op_Type, String Op_Comment, String Oper_Type, String Oper_Target, String Status, String Old_Param, String New_Param) {
        try {
            String Log_Guid = UUID.randomUUID().toString();
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String Oper_Time = simpleDateFormat.format(date);

            String updateSQL = "Insert Into Sys_Oper_Log (Oper_Id, Oper_No, Title, Op_Type, Op_Comment, Oper_Type, Oper_Target, Old_Param, New_Param, Rolegroup_Id, Staff_Id, Status, Oper_Time) Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            jt.update(updateSQL, Log_Guid, Rec_Guid, "法大大批量合同Log", Op_Type, Op_Comment, Oper_Type, Oper_Target, Old_Param, New_Param, "######", "######", Status, Oper_Time);

        } catch (Exception e) {
            log.error("LogToDB Exception:", e);
        }
    }

    // 记录大白菜一体化系统客户信息Log
    public static void toDBCCustInfoLog(JdbcTemplate jt, String Rec_Guid, JSONObject res_Json) {
        try {

            String Log_Guid = UUID.randomUUID().toString();
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String Oper_Time = simpleDateFormat.format(date);

            String updateSQL = "Insert Into Sys_Oper_Log_Dbc (Oper_Id, Rec_Guid, Title, Customerid, Customeroid, Customername, Companyname, Sex, Mobile, Customertypename, Idno, Address, Feedwname, Iscompany, Politicdesc, Unicode, Oper_Time, Status) Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)";

            jt.update(updateSQL, Log_Guid, Rec_Guid, "记录大白菜一体化系统客户信息Log", KHUtil.GetSFJV(res_Json, "customerid"), KHUtil.GetSFJV(res_Json, "customeroid"), KHUtil.GetSFJV(res_Json, "customername"), KHUtil.GetSFJV(res_Json, "companyname"), KHUtil.GetSFJV(res_Json, "sex"), KHUtil.GetSFJV(res_Json, "mobile"), KHUtil.GetSFJV(res_Json, "customertypename"), KHUtil.GetSFJV(res_Json, "idno"), KHUtil.GetSFJV(res_Json, "address"), KHUtil.GetSFJV(res_Json, "feedwname"), KHUtil.GetSFJV(res_Json, "iscompany"), KHUtil.GetSFJV(res_Json, "politicDesc"), KHUtil.GetSFJV(res_Json, "unicode"), Oper_Time);

            jt.update("Update Sys_Oper_Log_Dbc Set Status = -1 Where Status = 1 And Rec_Guid = ? And Oper_Id != ?", Rec_Guid, Log_Guid);

        } catch (Exception e) {
            log.error("记录大白菜一体化系统客户信息Log Exception:", e);
        }
    }

    // 记录FDD-系统客户信息Log
    public static void toFDDCustInfoLog(JdbcTemplate jt, String Rec_Guid, JSONObject res_Json) {
        try {
            String res_Code = KHUtil.GetSFJV(res_Json, "code");

            if (res_Code.equals("1")) {
                JSONArray dataArr = res_Json.getJSONArray("data");
                if (dataArr.size() > 0) {
                    for (int i = 0; i < dataArr.size(); i++) {
                        JSONObject user_Json = dataArr.getJSONObject(i);

                        String Id = KHUtil.GetSFJV(user_Json, "id");
                        String Customerid = KHUtil.GetSFJV(user_Json, "customerId");
                        String Verifiedstatus = KHUtil.GetSFJV(user_Json, "verifiedStatus");
                        String Verifiedstatusdesc = KHUtil.GetSFJV(user_Json, "verifiedStatusDesc");

                        String Verified_Name = KHUtil.GetSFJV(user_Json.getJSONObject("personVerifiedInfoVO"), "name");
                        String Verified_Identtype = KHUtil.GetSFJV(user_Json.getJSONObject("personVerifiedInfoVO"), "identType");
                        String Verified_Identno = KHUtil.GetSFJV(user_Json.getJSONObject("personVerifiedInfoVO"), "identNo");
                        String Verified_Mobile = KHUtil.GetSFJV(user_Json.getJSONObject("personVerifiedInfoVO"), "mobile");
                        String Verified_Email = KHUtil.GetSFJV(user_Json.getJSONObject("personVerifiedInfoVO"), "email");
                        String Verified_Bankno = KHUtil.GetSFJV(user_Json.getJSONObject("personVerifiedInfoVO"), "bankNo");
                        String Verified_Address = KHUtil.GetSFJV(user_Json.getJSONObject("personVerifiedInfoVO"), "address");
                        String Verified_Way = KHUtil.GetSFJV(user_Json.getJSONObject("personVerifiedInfoVO"), "verifiedWay");
                        String Verified_Audittime = KHUtil.GetSFJV(user_Json.getJSONObject("personVerifiedInfoVO"), "auditTime");
                        String Verified_Auditfailreason = KHUtil.GetSFJV(user_Json.getJSONObject("personVerifiedInfoVO"), "auditFailReason");

                        String Log_Guid = UUID.randomUUID().toString();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String Oper_Time = simpleDateFormat.format(new Date());

                        String updateSQL = "Insert Into Sys_Oper_Account_Person (Oper_Id, Rec_Guid, Id, Customerid, Verifiedstatus, Verifiedstatusdesc, Verified_Name, Verified_Identtype, Verified_Identno, Verified_Mobile, Verified_Email, Verified_Bankno, Verified_Address, Verified_Way, Verified_Audittime, Verified_Auditfailreason, Remark, Oper_Time, Status) Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)";

                        jt.update(updateSQL, Log_Guid, Rec_Guid, Id, Customerid, Verifiedstatus, Verifiedstatusdesc, Verified_Name, Verified_Identtype, Verified_Identno, Verified_Mobile, Verified_Email, Verified_Bankno, Verified_Address, Verified_Way, Verified_Audittime, Verified_Auditfailreason, Verifiedstatusdesc, Oper_Time);

                        jt.update("Update Sys_Oper_Account_Person Set Status = -1 Where Status = 1 And Rec_Guid = ? And Oper_Id != ?", Rec_Guid, Log_Guid);
                    }
                } else {
                    String Log_Guid = UUID.randomUUID().toString();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String Oper_Time = simpleDateFormat.format(new Date());

                    String updateSQL = "Insert Into Sys_Oper_Account_Person (Oper_Id, Rec_Guid, Remark, Oper_Time, Status) Values (?, ?, ?, ?, 1)";

                    jt.update(updateSQL, Log_Guid, Rec_Guid, "未注册", Oper_Time);

                    jt.update("Update Sys_Oper_Account_Person Set Status = -1 Where Status = 1 And Rec_Guid = ? And Oper_Id != ?", Rec_Guid, Log_Guid);

                }
            }

        } catch (Exception e) {
            log.error("记录FDD系统客户信息Log Exception:", e);
        }
    }

    // 记录FDD-系统公司信息Log
    public static void toFDDCompInfoLog(JdbcTemplate jt, String Rec_Guid, JSONObject res_Json) {
        try {
            String res_Code = KHUtil.GetSFJV(res_Json, "code");

            if (res_Code.equals("1")) {
                JSONArray dataArr = res_Json.getJSONArray("data");
                for (int i = 0; i < dataArr.size(); i++) {
                    JSONObject data_Json = dataArr.getJSONObject(i);

                    String Customerid = KHUtil.GetSFJV(data_Json, "customerId");

                    String Admininfo_Customerid = KHUtil.GetSFJV(data_Json.getJSONObject("companyAdminInfoVO"), "customerId");
                    String Admininfo_Account = KHUtil.GetSFJV(data_Json.getJSONObject("companyAdminInfoVO"), "account");

                    JSONObject companyVerifiedStatusVO = data_Json.getJSONObject("companyVerifiedStatusVO");

                    String Verifiedstatus = KHUtil.GetSFJV(companyVerifiedStatusVO, "verifiedStatus");
                    String Verifiedstatusdesc = KHUtil.GetSFJV(companyVerifiedStatusVO, "verifiedStatusDesc");
                    String Companyverifiedinfovo = KHUtil.GetSFJV(companyVerifiedStatusVO, "companyVerifiedInfoVO");
                    String Personverifiedinfovo = KHUtil.GetSFJV(companyVerifiedStatusVO, "personVerifiedInfoVO");

                    String Log_Guid = UUID.randomUUID().toString();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String Oper_Time = simpleDateFormat.format(new Date());

                    String updateSQL = "Insert Into Sys_Oper_Account_Company (Oper_Id, Rec_Guid, Customerid, Admininfo_Customerid, Admininfo_Account, Verifiedstatus, Verifiedstatusdesc, Companyverifiedinfovo, Personverifiedinfovo, Remark, Oper_Time, Status) Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)";

                    jt.update(updateSQL, Log_Guid, Rec_Guid, Customerid, Admininfo_Customerid, Admininfo_Account, Verifiedstatus, Verifiedstatusdesc, Companyverifiedinfovo, Personverifiedinfovo, data_Json.toString(), Oper_Time);

                    jt.update("Update Sys_Oper_Account_Company Set Status = -1 Where Status = 1 And Rec_Guid = ? And Oper_Id != ?", Rec_Guid, Log_Guid);
                }


            }

        } catch (Exception e) {
            log.error("记录FDD系统客户信息Log Exception:", e);
        }
    }

    // 记录FDD-合同签署状态详情Log
    public static void toFDDSignLog(JdbcTemplate jt, String Rec_Guid, JSONObject res_Json) {
        try {
            // 初始化Log参数
            String Sign_Guid = UUID.randomUUID().toString();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String Oper_Time = simpleDateFormat.format(new Date());

            String res_Code = KHUtil.GetSFJV(res_Json, "code");

            if (res_Code.equals("1")) {
                JSONObject data_Json = res_Json.getJSONObject("data");

                String docNo = KHUtil.GetSFJV(data_Json, "docNo");
                String docTitle = KHUtil.GetSFJV(data_Json, "docTitle");
                String sendTime = KHUtil.GetSFJV(data_Json, "sendTime");
                String senderName = KHUtil.GetSFJV(data_Json, "senderName");
                String signStatus = KHUtil.GetSFJV(data_Json, "status");
                String signStatusDesc = KHUtil.GetSFJV(data_Json, "statusDesc");
                String signKind = KHUtil.GetSFJV(data_Json, "signKind");
                String contractTypeId = KHUtil.GetSFJV(data_Json, "contractTypeId");
                String contractTypeName = KHUtil.GetSFJV(data_Json, "contractTypeName");
                String contractExpireDate = KHUtil.GetSFJV(data_Json, "contractExpireDate");


                String updateSQL = "Insert Into Sys_Oper_Log_Sign (Oper_Id, Rec_Guid, Docno, Doctitle, Sendtime, Sendername, Signstatus, Signstatusdesc, Signkind, Contracttypeid, Contracttypename, Contractexpiredate, Oper_Time, Status) Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1) ";

                jt.update(updateSQL, Sign_Guid, Rec_Guid, docNo, docTitle, sendTime, senderName, signStatus, signStatusDesc, signKind, contractTypeId, contractTypeName, contractExpireDate, Oper_Time);

                jt.update("Update Sys_Oper_Log_Sign Set Status = -1 Where Status = 1 And Rec_Guid = ? And Oper_Id != ?", Rec_Guid, Sign_Guid);
                jt.update("Update Sys_Oper_Log_Sign_Detail Set Status = -1 Where Status = 1 And Rec_Guid = ? And Sign_Guid != ?", Rec_Guid, Sign_Guid);

                JSONArray signerArr = data_Json.getJSONArray("contractSignerList");
                for (int i = 0; i < signerArr.size(); i++) {
                    JSONObject sign_Json = signerArr.getJSONObject(i);

                    String customerId = KHUtil.GetSFJV(sign_Json, "customerId");
                    String companyName = KHUtil.GetSFJV(sign_Json, "companyName");
                    String account = KHUtil.GetSFJV(sign_Json, "account");
                    String name = KHUtil.GetSFJV(sign_Json, "name");
                    String sign_Status = KHUtil.GetSFJV(sign_Json, "signStatus");
                    String sign_StatusDesc = KHUtil.GetSFJV(sign_Json, "signStatusDesc");
                    String signTime = KHUtil.GetSFJV(sign_Json, "signTime");
                    String isSender = KHUtil.GetSFJV(sign_Json, "isSender");
                    String signerRole = KHUtil.GetSFJV(sign_Json, "signerRole");
                    String mobile = KHUtil.GetSFJV(sign_Json, "mobile");
                    String email = KHUtil.GetSFJV(sign_Json, "email");
                    String sealName = KHUtil.GetSFJV(sign_Json, "sealName");

                    updateSQL = "Insert Into Sys_Oper_Log_Sign_Detail (Oper_Id, Rec_Guid, Sign_Guid, Customerid, Companyname, Account, Name, Signstatus, Signstatusdesc, Signtime, Issender, Signerrole, Mobile, Email, Sealname, Oper_Time, Status) Values (Uuid(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)";

                    jt.update(updateSQL, Rec_Guid, Sign_Guid, customerId, companyName, account, name, sign_Status, sign_StatusDesc, signTime, isSender, signerRole, mobile, email, sealName, Oper_Time);

                }

            }

        } catch (Exception e) {
            log.error("记录法大大-合同签署状态详情Log Exception:", e);
        }
    }
}